package com.example.demo.service.impl;

import com.example.demo.dao.CommentRepository;
import com.example.demo.entities.Comment;
import com.example.demo.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.Lob;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class CommentServiceImpl implements CommentService {


    @Autowired
    private CommentRepository commentRepository;

    @Override
    public Comment saveComment(Comment comment) {//保存评论 保存评论的时候要把评论的时间也保存下来
        Long parentCommentId = comment.getParentComment().getId();//获取父评论的id
        if(parentCommentId!=-1){  //有父级评论
            comment.setParentComment(commentRepository.findById(parentCommentId).orElse(null));//设置父评论
        }else{//没有父级评论
            comment.setParentComment(null);//设置父评论为空
        }//设置评论的时间
        comment.setCreateTime(new Date());
        return commentRepository.save(comment);//保存评论
    }

    @Override
    public List<Comment> listCommentByNewId(Long newId) {//根据新闻id查询评论
        Sort sort = Sort.by(Sort.Direction.ASC,"createTime");//按照评论的时间排序
//        Sort sort = Sort.by("createTime");//按照评论的时间排序
        List<Comment> comments = commentRepository.findByNewsIdAndParentCommentNull(newId,sort);//根据新闻id和父评论为空查询评论
        System.out.println("运行到listCommentByNewId");//测试
        return eachComment(comments);//返回评论
    }

    //循环查找所有评论
    private List<Comment> eachComment(List<Comment> comments){
        List<Comment> commentsView = new ArrayList<>();//创建一个新的集合
        System.out.println("eachComment");//测试
        for(Comment comment:comments){//遍历评论
            Comment c = new Comment();//创建一个新的评论
            BeanUtils.copyProperties(comment,c);//复制评论
            commentsView.add(c);//添加评论
        }
        //合并评论的各层子代到第一代集合
        combineChildrenComment(commentsView);
        return commentsView;
    }

    private void combineChildrenComment(List<Comment> comments){//合并评论的各层子代到第一代集合
        System.out.println("combineChildrenComment");
        for(Comment comment:comments){
            List<Comment> replies = comment.getReplyComments();
            for(Comment reply : replies){
                //循环迭代，找出子代  存放在临时存放区
                recursively(reply);
            }
            comment.setReplyComments(tempReplies);
            //清除临时存放区
            tempReplies = new ArrayList<>();
        }
    }

    //临时找出子代
    private List<Comment> tempReplies = new ArrayList<>();//创建一个新的集合

    private void recursively(Comment comment){//循环迭代，找出子代
        tempReplies.add(comment);  //顶节点添加到临时存放区
        if(comment.getReplyComments().size()>0){
            List<Comment> replies = comment.getReplyComments();
            for(Comment reply:replies){
                tempReplies.add(reply);
                if(comment.getReplyComments().size()>0){
                    recursively(reply);
                }
            }
        }
    }

}
